<?php
namespace App\Http\Controllers\Booked;

use App\Http\Controllers\Controller;
use App\Jobs\PushMsgJob;
use App\Jobs\SendNormalMsgJob;
use App\Jobs\SMSJob;
use App\Libraries\Decorator\Image\CoverDecorator;
use App\Libraries\Decorator\Tool\IsEndDecorator;
use App\Libraries\Tools\ConstVar;
use App\Libraries\Tools\Msg;
use App\Models\Banner\Banner;
use App\Models\Booked\Booked;
use App\Models\Shop\ShopManagerFullDetail;
use App\Models\User\SMS;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Validator;


class BookedController extends Controller{


    private $booked;
    private $isEndDecorator;
    private $coverDecorator;

    public function __construct(Booked $booked, IsEndDecorator $isEndDecorator,
                                CoverDecorator $coverDecorator){
        $this->booked = $booked;
        $this->isEndDecorator = $isEndDecorator;
        $this->coverDecorator = $coverDecorator;
    }

    /**
     * 预定列表
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function booked(Request $request){
        $validator = Validator::make($request->all(), [
            'type' => 'required|integer|in:1,2',//1-一般用户 2-经理
            'shop_id' => 'required_if:type,2|integer',
            'page' => 'required|integer',
            'size' => 'required|integer',
        ], [
            'type.required' => '10001',
            'type.integer' => '10002',
            'type.in' => '10002',
            'shop_id.required_if' => '10001',
            'shop_id.integer' => '10002',
            'page.required' => '10001',
            'page.integer' => '10002',
            'size.required' => '10001',
            'size.integer' => '10002',
        ]);
        if ($validator->fails()) {
            return $this->sendFailResponse(400, $validator->errors()->first());
        }

        $this->booked->addDecorator($this->isEndDecorator);
        $this->booked->addDecorator($this->coverDecorator);
        if($request['type'] == 1){
            $data = $this->booked->user_booked($request['page'], $request['size'], Auth::id());
        }else{
            $data =  $this->booked->manager_booked($request['page'], $request['size'], Auth::id(), $request['shop_id']);
        }
        return $this->sendSuccessResponse($data);

    }

    /**
     * 添加预定
     * @param Request $request
     * @param ShopManagerFullDetail $shopManagerFullDetail
     * @return \Illuminate\Http\JsonResponse
     */
    public function add(Request $request, ShopManagerFullDetail $shopManagerFullDetail){
        $validator = Validator::make($request->all(), [
            'shop_id' => 'required|integer',
            'manager_user_id' => 'required|integer',
            'mobile' => ['regex:/^1[34578][0-9]{9}$/', 'required'],
            'num' => 'required|integer',
            'time' => 'required|integer',
        ], [
            'shop_id.required' => '10001',
            'shop_id.integer' => '10002',
            'manager_user_id.required' => '10001',
            'manager_user_id.integer' => '10002',
            'mobile.required' => '10001',
            'mobile.regex' => '10002',
            'num.required' => '10001',
            'num.integer' => '10002',
            'time.required' => '10001',
            'time.integer' => '10002',
        ]);
        if ($validator->fails()) {
            return $this->sendFailResponse(400, $validator->errors()->first());
        }

        $manager = $shopManagerFullDetail
            ->where('shop_id', $request['shop_id'])
            ->where('user_id', $request['manager_user_id'])
            ->where('status', 1)
            ->first();
        if(empty($manager)){
            return $this->sendFailResponse(400, 40400);
        }

        $this->booked->user_id = Auth::id();
        $this->booked->shop_id = $request['shop_id'];
        $this->booked->manager_user_id = $request['manager_user_id'];
        $this->booked->mobile = $request['mobile'];
        $this->booked->num = $request['num'];
        $this->booked->time = $request['time'];
        $this->booked->save();

        if(!empty($manager->mobile)){
            dispatch(new SMSJob($manager->mobile, Msg::booked_manager($manager->title)));
        }
        dispatch(new SendNormalMsgJob($manager->user_id, Msg::booked_manager($manager->title)));
        dispatch(new PushMsgJob([$manager->user_id], Msg::booked_manager($manager->title), 0, ['user_id'=>8, 'type'=>0]));
        return $this->sendSuccessResponse([]);
    }

    /**
     * 修改
     * @param $id
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function edit($id, Request $request){
        $valid_data = $request->all();
        $valid_data['id'] = $id;
        $validator = Validator::make($valid_data, [
            'id' => 'required|integer',
            'status' => 'required|integer|in:1,2,4', //1-同意 2-拒绝 4-取消
            'comment' => 'required_if:status,2|string|max:50'
        ], [
            'id.required' => '10001',
            'id.integer' => '10002',
            'status.required' => '10001',
            'status.integer' => '10002',
            'status.in' => '10002',
            'comment.required_if' => '10001',
            'comment.string' => '10002',
            'comment.max' => '10002',
        ]);
        if ($validator->fails()) {
            return $this->sendFailResponse(400, $validator->errors()->first());
        }

        $booked = $this->booked->where('id', $id)->first();
        if(empty($booked)){
            return $this->sendFailResponse(400, 40400);
        }

        if($booked->status != ConstVar::$BOOKED_STATUS_WAITING){
            return $this->sendFailResponse(400, 40001);
        }

        if($request['status'] == ConstVar::$BOOKED_STATUS_CANCEL){
            if($booked->user_id != Auth::id()){
                return $this->sendFailResponse(400, 40101);
            }
        }else{
            if($booked->manager_user_id != Auth::id()){
                return $this->sendFailResponse(400, 40101);
            }
        }

        $booked->status = $request['status'];
        switch($request['status']){
            case ConstVar::$BOOKED_STATUS_AGREE:
                dispatch(new SMSJob($booked->mobile, Msg::booked_agree()));
                dispatch(new SendNormalMsgJob($booked->user_id, Msg::booked_agree()));
                break;
            case ConstVar::$BOOKED_STATUS_REJECT:
                $booked->comment = $request['comment'];
                dispatch(new SMSJob($booked->mobile, Msg::booked_reject($request['comment'])));
                dispatch(new SendNormalMsgJob($booked->user_id, Msg::booked_reject($request['comment'])));
                break;
            default:
                break;
        }
        $booked->save();
        return $this->sendSuccessResponse([]);
    }
}